package com.seguriapp.activities;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.seguriapp.R;
import com.seguriapp.controllers.TramitesController;
import com.seguriapp.entities.TramiteAttributeEntity;
import com.seguriapp.sync.TramiteFileTask;

public class TramiteDetalleActivity extends Activity {

	private Context mContext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tramitedetalle);
		mContext = this;
		if (getIntent().getExtras().getBoolean("hasFile")) {
			Button btnGenerar = ((Button) findViewById(R.id.btnGenerar));
			btnGenerar.setVisibility(View.VISIBLE);
			btnGenerar.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					showGenerateDialog();
				}

			});
		}
		setData();
	}

	private void showGenerateDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		LayoutInflater inflater = getLayoutInflater();
		final View view = inflater.inflate(R.layout.dialog_tramite_file, null);
		builder.setView(view)
				// Add action buttons
				.setPositiveButton("Generar",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int id) {
								final String dni = ((EditText) view
										.findViewById(R.id.txtDni)).getText()
										.toString();
								if (dni.length() < 8) {
									Toast.makeText(mContext,
											"El DNI debe tener 8 digitos",
											Toast.LENGTH_SHORT).show();
								} else {
									final TramiteFileTask task = new TramiteFileTask(
											mContext, "/"
													+ getIntent().getExtras()
															.getInt("id") + "/"
													+ dni);
									task.execute();
								}
							}
						})
				.setNegativeButton("Cancelar",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.dismiss();
							}
						});
		builder.create().show();
	}

	private void setData() {
		final Bundle extra = this.getIntent().getExtras();
		((TextView) findViewById(R.id.tramiteTitle)).setText(extra
				.getString("name"));
		long tramiteId = extra.getLong("tramiteId");

		if (extra.containsKey("cost")) {
			((TextView) findViewById(R.id.tramiteCost)).setText(extra
					.getString("cost"));
		}

		if (extra.containsKey("duration")) {
			((TextView) findViewById(R.id.tramiteDuration)).setText(extra
					.getString("duration"));
		}

		final HashMap<String, ArrayList<TramiteAttributeEntity>> attributesList = new TramitesController(
				this).getTramitesLists(tramiteId);
		final ArrayList<TramiteAttributeEntity> lstObjectives = attributesList
				.get("objectives");
		if (lstObjectives.size() > 0) {
			final LinearLayout linearObjectives = (LinearLayout) findViewById(R.id.tramiteObjectives);
			for (int i = 0; i < lstObjectives.size(); i++) {
				final TextView text = new TextView(this);
				text.setText(lstObjectives.get(i).getValue());
				text.setTextAppearance(this, R.style.detalleTramiteInfo);
				linearObjectives.addView(text);
			}
		}

		final ArrayList<TramiteAttributeEntity> lstFilesNeeded = attributesList
				.get("filesNeeded");
		if (lstFilesNeeded.size() > 0) {
			final LinearLayout linearFiles = (LinearLayout) findViewById(R.id.tramiteFilesNeeded);
			for (int i = 0; i < lstFilesNeeded.size(); i++) {
				final TextView text = new TextView(this);
				text.setText(lstFilesNeeded.get(i).getValue());
				text.setTextAppearance(this, R.style.detalleTramiteInfo);
				linearFiles.addView(text);
			}
		}

		final ArrayList<TramiteAttributeEntity> lstObservations = attributesList
				.get("observations");
		if (lstObservations.size() > 0) {
			final LinearLayout linearFiles = (LinearLayout) findViewById(R.id.tramiteObservation);
			for (int i = 0; i < lstObservations.size(); i++) {
				final TextView text = new TextView(this);
				text.setText(lstObservations.get(i).getValue());
				text.setTextAppearance(this, R.style.detalleTramiteInfo);
				linearFiles.addView(text);
			}
		}

		final ArrayList<TramiteAttributeEntity> lstPlace = attributesList
				.get("place");
		if (lstPlace.size() > 0) {
			final LinearLayout linearFiles = (LinearLayout) findViewById(R.id.tramitePlace);
			for (int i = 0; i < lstPlace.size(); i++) {
				final TextView text = new TextView(this);
				text.setText(lstPlace.get(i).getValue());
				text.setTextAppearance(this, R.style.detalleTramiteInfo);
				linearFiles.addView(text);
			}
		}
	}

}
